package com.gitee.ice1938.adcode.fun;

import java.util.Calendar;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gitee.ice1938.mvc.base.AjaxResult;
import com.gitee.ice1938.mvc.base.PageConvert;

import net.datatables.ajax.FormData;
import net.datatables.ajax.PageImpl;

/**
 */
@Controller
//@RequestMapping("")
public class DictAdministrativeAreaController {

	@Autowired
	private DictAdministrativeAreaService dictAdministrativeAreaService;

	@Autowired
	private CacheManager cacheManager;

	/**
	 * 页面
	 */
	@RequestMapping(path = {"/index","","/"})
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "manage/dict/administrative_area";
	}

	/**
	 * 数据查询列表
	 */
	@RequestMapping(path = "/list")
	@ResponseBody
	public PageImpl<DictAdCodeModel> list(HttpServletRequest request, HttpServletResponse response) {
		FormData formData = new FormData(request);
		Map<String, Object> params = formData.getParam();
		Pageable pagebale = PageConvert.convertPageable(formData);
		Order order =PageConvert.convertOrder(formData);
		Page<DictAdCodeModel> page = dictAdministrativeAreaService.queryPage(params,pagebale,order);

		return PageConvert.convertPage(page);
	}

	/**
	 * 重新加载缓存数据
	 * 
	 * @return
	 */
	@RequestMapping("/reload_cache")
	@ResponseBody
	public AjaxResult reloadCache() {
		Collection<String> list = cacheManager.getCacheNames();
		for (String str : list) {
			Cache cache = cacheManager.getCache(str);
			cache.clear();
		}
		Calendar cl = Calendar.getInstance();
		dictAdministrativeAreaService.queryForCityselect(cl.get(Calendar.YEAR));
		return AjaxResult.ok();
	}

}
